package toy.keli.picmap.util;

import android.annotation.SuppressLint;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;

public class utilArray {

   public  static String idzip (int[] ids){
        String id="";
        Arrays.sort(ids);
        boolean rangFlag=false;
        for(int i=0,bid=0;i<ids.length;i++)
        {
            int v=ids[i];
            if(bid==v-1){
                if(!rangFlag && !id.equals("")) id=id.substring(0,id.length()-1)+":";
                rangFlag=true;
                bid=v;
                continue;
            }else if(bid>0 && rangFlag){
                id+=bid+",";
                rangFlag=false;
            }
            bid=v;
            id+=v+",";
        }
        if(rangFlag) id+=ids[ids.length-1];
        return id;
    }

    @SuppressLint("NewApi")
    public static String idunzip(String id) {
        List<Integer> rt=new ArrayList<>();
        if(id.length()==0) return id;
        String[] a = id.split(",");
        for(int i=0;i<a.length;i++){
            if(a[i].indexOf(':')!=-1){
                String[] b = a[i].split(":");
                int[] ra={Integer.valueOf(b[0]),Integer.valueOf(b[1])+1};
                //https://www.cnblogs.com/niejunlei/p/12482117.html
                IntStream.range(ra[0], ra[1]).forEach(v->rt.add(v));
            }else{
                rt.add(Integer.valueOf(a[i]));
            }
        }
//        int[] ids=new int[rt.size()];
//        for(int i=0;i<ids.length;i++){
//            ids[i]=rt.get(i);
//        }
        //https://www.cnblogs.com/cat520/p/10299879.html
        String[] rts=new String[rt.size()];
        for(int i=0;i<rts.length;i++){
            rts[i]= String.valueOf(rt.get(i));
        }
        String ids=String.join(",",rts);
        return ids;
    }

    public static int indexOf(String[] dates, String title) {
       for(int i=0;i<dates.length;i++){
           if(dates[i].equals(title)) return i;
       }
       return -1;
    }

    public static int indexOf(int[] dates, int title) {
        for(int i=0;i<dates.length;i++){
            if(dates[i]==title) return i;
        }
        return -1;
    }

    public static void reverse(Object[] dates) {
       for(int i=0,j=0;i<dates.length/2;i++){
           Object tmp=dates[i];
           j=dates.length-i-1;
           dates[i]=dates[j];
           dates[j]=tmp;
       }
    }
}
